package edu.jit.flowers.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import edu.jit.flowers.bean.Carts;
import edu.jit.flowers.utils.JDBCUtil;

public class CartsDAO {
	//添加入购物车
	public int addCarts(Carts c) {
		int rows=0;
		String sql="insert into carts values(0,?,?,?,?,?,?,?)";
		QueryRunner runner=new QueryRunner(JDBCUtil.getDataSource());
		 try {
			rows=runner.execute(sql, c.getCartUId(),c.getCartFId(),
					 c.getCartAmount(),c.getCartTotal(),c.getCartPrice(),
					 c.getCartFName(),c.getCartFPic());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}
	//根据花名删除购物车商品
	 public int deleteCarts(String fname) {
	   	 int rows=0;
	   	 String sql="delete from carts where cart_f_name=?";
	   	 QueryRunner runner=new QueryRunner(JDBCUtil.getDataSource());
	   	 try {
				rows=runner.update(sql,fname);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	   	 return rows;
	    }
	//根据购物车里的用户名修改购物车里商品
	 public int updateCarts(Carts c) {
	   	 int rows=0;
	   	 String sql="update carts set cart_id=?,cart_u_id=?,cart_f_id=?,"
	   	 		+ "cart_amount=?,cart_total=?,cart_price=?,cart_f_name=?,cart_f_pic=?"
	   	 		+ "where cart_u_id=?";
	   	 QueryRunner runner=new QueryRunner(JDBCUtil.getDataSource());
	   	 try {
				rows=runner.update(sql,c.getCartId(),c.getCartUId(),c.getCartFId(),
						 c.getCartAmount(),c.getCartTotal(),c.getCartPrice(),
						 c.getCartFName(),c.getCartFPic(),c.getCartUId());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	   	 return rows;
	    }
	 
	//根据用户id查看购物车商品
	 public List<Carts> selectAll(int uId){
			List<Carts> carts = new ArrayList<>();
			String sql="select cart_id as cartid,cart_u_id as cartuid,"
					+ "cart_f_id as cartfid,cart_amount as cartamount,cart_total as carttotal,"
					+ "cart_price as cartprice,cart_f_name as cartfname,cart_f_pic ascartfpic"
					+ "  from carts where cart_u_id=?";
			QueryRunner runner=new QueryRunner(JDBCUtil.getDataSource());
			try {
				carts=runner.query(sql, new BeanListHandler<>(Carts.class),uId);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       return carts;
	    }
	//根据购物车id查看购物车商品
		 public Carts selectAllByCId(int cartid){
				Carts carts = new Carts();
				String sql="select cart_id as cartid,cart_u_id as cartuid,"
						+ "cart_f_id as cartfid,cart_amount as cartamount,cart_total as carttotal,"
						+ "cart_price as cartprice,cart_f_name as cartfname,cart_f_pic ascartfpic"
						+ "  from carts where cart_id=?";
				QueryRunner runner=new QueryRunner(JDBCUtil.getDataSource());
				try {
					carts=runner.query(sql, new BeanHandler<>(Carts.class),cartid);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		       return carts;
		    }
	 
}
